package com.yike.user.strategy.login.source;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yike.common.exception.BusinessException;
import com.yike.common.tool.StringUtil;
import com.yike.common.vo.LoginUserInfoVo;
import com.yike.user.entity.Merchant;
import com.yike.user.mapper.MerchantMapper;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Sakura
 * @date 2024/8/20 11:26
 * 账号密码登录
 */
@Component
@Log
public class MerchantLoginStrategy implements LoginSourceStrategy {

    @Autowired
    private MerchantMapper merchantMapper;


    @Override
    public void login(LoginUserInfoVo loginUserInfoVo) {
        // 保存用户类型
        loginUserInfoVo.setType(2);
        // 获取用户商户信息
        Merchant merchant = merchantMapper.selectOne(Wrappers.lambdaQuery(Merchant.class)
                .eq(Merchant::getUserId, loginUserInfoVo.getId()));
        if (StringUtil.isNotBlank(loginUserInfoVo.getMobile()) && merchant == null) {
            throw new BusinessException("您还没有成为商家，请联系平台客服成为商家");
        }
        if (merchant != null) {
            // 验证商户状态
            if (merchant.getStatus() == 2) {
                throw new BusinessException("当前商户已被禁用，请联系平台客服");
            }
            // 添加商户号到登录信息
            loginUserInfoVo.setMerchantId(merchant.getId());
            loginUserInfoVo.setMerchantNo(merchant.getMerchantNo());
        }
    }
}
